package edu.jsnu.purchase.ticket.filter;

import edu.jsnu.service.beans.RequestHolder;
import edu.jsnu.service.ticket.user.domain.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        User sysUser = (User) req.getSession().getAttribute("user");
        RequestHolder.add(req);
        if (sysUser == null) {
            resp.sendRedirect(req.getContextPath()+"/login/login.page");
            return;
        }
        RequestHolder.add(sysUser);
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    @Override
    public void destroy() {

    }
}
